package com.fruitstore.jdbc1.dao.impl;



import com.bookstore.utils.DruidUtils;
import com.fruitstore.jdbc1.pojo.Fruit;
import com.fruitstore.jdbc1.dao.FruitDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
// 实现数据库CRUD操作
public class FruitDaoImpl implements FruitDao {
    Connection conn;
    PreparedStatement psmt;
    ResultSet rs;

    @Override
    public List<Fruit> getFruitList() {
        List<Fruit> fruitList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            // 3.编写SQL语句
            String sql = "select * from t_fruit";
            // 4.创建预处理命令对象
            psmt = conn.prepareStatement(sql);
            // 5.执行查询
            rs = psmt.executeQuery();
            // 6.解析结果
            while (rs.next()){
                int fid = rs.getInt(1);
                String fname = rs.getString(2);
                int price = rs.getInt(3);
                int fcount = rs.getInt(4);
                String remark = rs.getString(5);
                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);
                fruitList.add(fruit);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DruidUtils.close(conn,psmt,rs);
        }
        return fruitList;
    }

    @Override
    public boolean addFruit(Fruit fruit) {
        try {
            conn = DruidUtils.getConnection();
            // 3.编写SQL语句
            String sql = "insert into t_fruit values(0,?,?,?,?)";
            // 4.创建预处理命令对象
            psmt =conn.prepareStatement(sql);
            psmt.setString(1,fruit.getFname());
            psmt.setInt(2,fruit.getPrice());
            psmt.setInt(3,fruit.getFcount());
            psmt.setString(4,fruit.getFname());
            // 5.执行更新
            return psmt.executeUpdate()>0;

        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            DruidUtils.close(conn,psmt,rs);
        }
        return false;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {
        try {
            conn = DruidUtils.getConnection();
            // 3.编写SQL语句
            String sql = "update t_fruit set fcount = ? where fid = ?";
            // 4.创建预处理命令对象
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,fruit.getFcount());
            psmt.setInt(2,fruit.getFid());
            // 5.执行更新
            return psmt.executeUpdate()>0;

        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            DruidUtils.close(conn,psmt,rs);
        }
        return false;
    }

    @Override
    public boolean delFruit(String name) {
        try {
            conn = DruidUtils.getConnection();
            // 3.编写SQL语句
            String sql = "delete from t_fruit where fname like ?";
            // 4.创建预处理命令对象
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,name);
            // 5.执行更新
            return psmt.executeUpdate()>0;

        } catch (Exception e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        } finally {
            DruidUtils.close(conn,psmt,rs);
        }
        return false;
    }

    @Override
    public Fruit getFruitByFname(String fname) {
        try {
            conn = DruidUtils.getConnection();
            // 3.编写SQL语句
            String sql = "select * from t_fruit where fname=?";
            // 4.创建预处理命令对象
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,fname);
            // 5.执行查询
            rs = psmt.executeQuery();
            // 6.解析结果
            while (rs.next()){
                int fid = rs.getInt(1);
//                String getfname = rs.getString(2);
                int price = rs.getInt(3);
                int fcount = rs.getInt(4);
                String remark = rs.getString(5);
                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);
                return fruit;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(conn,psmt,rs);
        }
        return null;
    }
}
